// @ts-ignore
import { Effect, Reducer, Subscription, request } from 'umi';

export interface SongListModelState {
  catlistData: object;
  playlists: [];
  total: number;
  cat: string;
}

export interface SongListModelType {
  namespace: 'songList';
  state: SongListModelState;
  effects: any;
  reducers: any;
  subscriptions: any;
}

const SongListModel: SongListModelType = {
  namespace: 'songList',

  state: {
    catlistData: {},
    playlists: [],
    total: 0,
    cat: '',
  },

  effects: {
    *getCatlist({ type, payload }: any, { put, call, select }: any) {
      let data = yield request(`/playlist/catlist`);
      yield put({
        type: 'saveCatlist',
        payload: {
          catlistData: data,
        },
      });
    },
    *getPlaylist({ type, payload }: any, { put, call, select }: any) {
      let data = yield request(
        `/top/playlist?order=${payload.order}&cat=${payload.cat}&limit=${payload.limit}&offset=${payload.offset}`,
      );
      yield put({
        type: 'savePlaylist',
        payload: {
          playlists: data.playlists,
          total: data.total,
          cat: data.cat,
        },
      });
      return data;
    },
  },
  reducers: {
    saveCatlist(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
    savePlaylist(state: any, action: any) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {},
};

export default SongListModel;
